import os

BUILD_IOS_CMAKE_BASE = ""

# 编译用的线程数
BUILD_THREAD_SIZE = 4

NDK_ROOT = ''
# 设置Android 编译的 类型
# ANDROID_ABI_TYPE = "arm64-v8a"
# 设置 ios 平台类型
# IOS_PLATFORM_TYPR = "SIMULATOR64"

ANDROID_OPENSSL_CURL_ROOT ="/Volumes/wd320/wujehy/cproject/test_c/3rd_libs/Android"
# ANDROID_OPENSSL_CURL_ROOT ="/home/wujehy/devRoot/cpp/3rd_libs/Android"
BUILD_ANDROID_CMAKE_BASE = "cmake %s -DBUILD_SDK_TO_ANDROID=ON -DANDROID_NDK=%s -DCMAKE_TOOLCHAIN_FILE=%s/build/cmake/android.toolchain.cmake -DANDROID_ABI=\"%s\" -DOPENSSL_ROOT_DIR="+ANDROID_OPENSSL_CURL_ROOT+"/openssl/%s -DCURL_ROOT_DIR="+ANDROID_OPENSSL_CURL_ROOT+"/curl/%s -DANDROID_PLATFORM=android-24 -DANDROID_STL=\"c++_shared\" -DCMAKE_INSTALL_PREFIX=%s"
BUILD_IOS_CMAKE_BASE = "cmake %s -DBUILD_SDK_TO_IOS=ON -DCMAKE_TOOLCHAIN_FILE=%s/ios.toolchain.cmake -DPLATFORM=%s   -DIOS_ARCH=\"x86_64\" -DWITH_GFLAGS=OFF -DBUILD_TESTING=OFF -DBUILD_PC=OFF -DHAVE_DBGHELP=OFF -DCMAKE_INSTALL_PREFIX=%s"

#  -DCMAKE_INSTALL_PREFIX=/Volumes/wd320/wujehy/cproject/test_c/flutter-sdk-cpp/cmake-build-android_armv8/output
FLUTTER_SDK_SRC_PATH = ''


def init_env_path_data():
    global FLUTTER_SDK_SRC_PATH
    FLUTTER_SDK_SRC_PATH = os.getcwd()
    print("currenrt :", FLUTTER_SDK_SRC_PATH)

    getNDK_ROOT =os.getenv('NDK_ROOT')
    while getNDK_ROOT is None or getNDK_ROOT == "":
        print("not found NDK_ROOT_PATH :" , end="")
        getNDK_ROOT=input()

    else:
        print("find NDK_ROOT = " ,getNDK_ROOT )


    global NDK_ROOT
    NDK_ROOT = getNDK_ROOT
    print("BUILD_THREAD_SIZE (default : 4 ): " ,end="")
    inputSize = 4
    try:
        inputSize = int(input())
        print("your BUILD_THREAD_SIZE : ", inputSize )
    except:
        print("input fail using defatult 4 ")

    if inputSize > 1:
        BUILD_THREAD_SIZE = inputSize
    pass


# def mkdir_path(path):
#

def build_android_os_type_project(android_type):
    os.chdir(FLUTTER_SDK_SRC_PATH)
    print("start build android ")
    # 创建 工作路径
    CMAKE_BUILD_PATH = "%s/cmake_build" % FLUTTER_SDK_SRC_PATH
    isExists = os.path.exists(CMAKE_BUILD_PATH)
    if not isExists:
        print("cmake_build path is not exits ... ")
        os.makedirs(CMAKE_BUILD_PATH)
        print("create %s success ..." % CMAKE_BUILD_PATH)
    system_name = "%s_%s" % ("Android", android_type)
    CMAKE_BUILD_OBJ_PATH = "%s/%s" % (CMAKE_BUILD_PATH, system_name)
    isExists = os.path.exists(CMAKE_BUILD_OBJ_PATH)
    if not isExists:
        print("build_linux path is not exits ... ")
        os.makedirs(CMAKE_BUILD_OBJ_PATH)
        print("create %s success ..." % CMAKE_BUILD_OBJ_PATH)

    os.chdir(CMAKE_BUILD_OBJ_PATH)
    # 构建
    install_path = "%s/output" % (FLUTTER_SDK_SRC_PATH)

    cmake_build_cmd = BUILD_ANDROID_CMAKE_BASE % (FLUTTER_SDK_SRC_PATH, NDK_ROOT, NDK_ROOT, android_type,android_type,android_type, install_path)

    print("cmd : ", cmake_build_cmd)

    os.system(cmake_build_cmd)
    pass


def build_android_os_type_obj(android_type):
    print("build obj ")
    # 创建 工作路径
    CMAKE_BUILD_PATH = "%s/cmake_build" % FLUTTER_SDK_SRC_PATH
    system_name = "%s_%s" % ("Android", android_type)
    CMAKE_BUILD_OBJ_PATH = "%s/%s" % (CMAKE_BUILD_PATH, system_name)
    os.chdir(CMAKE_BUILD_OBJ_PATH)
    os.system("cmake --build .  -- -j %s" % BUILD_THREAD_SIZE)
    os.system("cmake --install . ")
    pass


def build_androd_so(buildtype):
    build_android_os_type_project(buildtype)
    build_android_os_type_obj(buildtype)


def build_apple_ios_type_project(build_type):
    print("start build apple ")
    os.chdir(FLUTTER_SDK_SRC_PATH)
    # 创建 工作路径
    CMAKE_BUILD_PATH = "%s/cmake_build" % FLUTTER_SDK_SRC_PATH
    isExists = os.path.exists(CMAKE_BUILD_PATH)
    if not isExists:
        print("cmake_build path is not exits ... ")
        os.makedirs(CMAKE_BUILD_PATH)
        print("create %s success ..." % CMAKE_BUILD_PATH)
    system_name = "%s_%s" % ("Apple", build_type)
    CMAKE_BUILD_OBJ_PATH = "%s/%s" % (CMAKE_BUILD_PATH, system_name)
    isExists = os.path.exists(CMAKE_BUILD_OBJ_PATH)
    if not isExists:
        print("build_linux path is not exits ... ")
        os.makedirs(CMAKE_BUILD_OBJ_PATH)
        print("create %s success ..." % CMAKE_BUILD_OBJ_PATH)

    os.chdir(CMAKE_BUILD_OBJ_PATH)

    install_path = "%s/output" % (FLUTTER_SDK_SRC_PATH)
    # 构建
    cmake_build_cmd = BUILD_IOS_CMAKE_BASE % (FLUTTER_SDK_SRC_PATH, FLUTTER_SDK_SRC_PATH, build_type, install_path)

    print("cmd : ", cmake_build_cmd)

    os.system(cmake_build_cmd)
    pass


def build_apple_ios_type_obj(build_type):
    print("build obj ")
    # 创建 工作路径
    CMAKE_BUILD_PATH = "%s/cmake_build" % FLUTTER_SDK_SRC_PATH
    system_name = "%s_%s" % ("Apple", build_type)
    CMAKE_BUILD_OBJ_PATH = "%s/%s" % (CMAKE_BUILD_PATH, system_name)
    os.chdir(CMAKE_BUILD_OBJ_PATH)
    os.system("cmake --build .  -- -j %s" % BUILD_THREAD_SIZE)

    os.system("cmake --install . ")
    pass


def build_apple_ios(build_type):
    build_apple_ios_type_project(build_type)
    build_apple_ios_type_obj(build_type)


if __name__ == "__main__":
    init_env_path_data()
    print("your NDK_ROOT : " ,NDK_ROOT)
    while True:
        print("cmd : ")
        # ANDROID_ABI_TYPE = "arm64-v8a"
        print("1 : build android so armeabi-v7a")
        print("2 : build android so arm64-v8a")
        print("3 : build android so x86")
        print("4 : build android so x86_64")
        print("5 : build ios dylib SIMULATOR64")
        print("q : exit")
        inputCommand = input()
        if inputCommand == "1":
            print("build android ")
            build_androd_so("armeabi-v7a")
            continue
        if inputCommand == "2":
            print("build android ")
            build_androd_so("arm64-v8a")
            continue
        if inputCommand == "3":
            print("build android ")
            build_androd_so("x86")
            continue
        if inputCommand == "4":
            print("build android ")
            build_androd_so( "x86_64")
            continue
        elif inputCommand == "5":
            build_apple_ios("SIMULATOR64")
            continue
        elif inputCommand == "q" or inputCommand == "Q":
            break
        else:
            print("no suppert")
            continue
